#include "head.h"
#include "thread_pool.h"
#include "sub_reactor.h"
#define MAX 50

void *sub_reactor(void *arg) {//从反应堆 采用reactor模式
	struct task_queue *taskQueue = (struct task_queue *)arg;//任务队列
	pthread_t *tid = (pthread_t *)calloc(MAXTHREAD, sizeof(pthread_t));//开适当个线程去取任务
	for (int i = 0; i < MAXTHREAD; i++) {
		pthread_create(&tid[i], NULL, thread_run, (void *)taskQueue);//每个线程去执行任务thread_run
	}
	struct epoll_event events[MAX];//从反应堆事件
	int nfds;
	while (1) {
		DBG(RED"Sub Reactor : "NONE"Epoll waiting...\n");
		nfds = epoll_wait(taskQueue->epollfd, events, MAX, -1);//当哪些文件描述符就绪的时候加到events中
		if (nfds == -1) {
			perror("epoll_wait");
			exit(1);
		}
		for (int i = 0; i < nfds; i++) {//有就绪的用户的时候
			struct User *user = (struct User *)events[i].data.ptr;//取出该用户
			DBG(L_YELLOW"events[n].data.ptr->name = %s \n"NONE, user->name);
			if (events[i].events & EPOLLIN) {//判断是否为来的事件
				task_queue_push(taskQueue, (struct User *)events[i].data.ptr);//如果是来的事件，那么加入要执行任务的队列中
			}
		}
	}
	return NULL;
}
